package com.codefactory.geneticalgorithm;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.ImprovedNamingStrategy;

/**
 * Utility class for Hibernate SessionFactory obtaining.
 * @author Oleg Kurbatov
 */
@SuppressWarnings("deprecation")
public class HibernateUtil {
    
    private static SessionFactory sessionFactory;
    
    static {
        sessionFactory = new Configuration()
                .setNamingStrategy(ImprovedNamingStrategy.INSTANCE)
                .configure()
                .buildSessionFactory();
    }
    
    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
    
    public static void save(Object o){
        Session s = sessionFactory.openSession();
        s.beginTransaction();
        try{
            s.save(o);
            s.getTransaction().commit();
        }finally{
            if(s.getTransaction().isActive()){
                s.getTransaction().rollback();
            }
            s.close();
        }
    }
    
}
